package executors.completable.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HandleTest001 {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    
    CompletableFuture<String> result = CompletableFuture.supplyAsync(() -> {
          System.out.println("基础线程A");
          return "A";
        }, executorService).thenApplyAsync((res) -> {
          System.out.println("基础线程B , 接受到返回值是: " + res);
          return "B" + res;
        }, executorService).thenApplyAsync((res) -> {
//          int count = 1 / 0;
          System.out.println("基础线程C , 接受到返回值是: " + res);
          return "C" + res;
        }, executorService)
        .handleAsync((r, ex) -> {
          System.out.println("这是whenCompleteAsync 返回结果: " + r);
          System.out.println("这是whenCompleteAsync 异常结果: " + ex);
          return "handle";
        }, executorService);
    
    System.out.println(result.get());
  }
}
